package apis

import (
	"fmt"
	"github.com/golang/protobuf/proto"
	"zinx/mmo_game_zinx/core"
	"zinx/mmo_game_zinx/pb"
	"zinx/ziface"
	"zinx/znet"
)

// MoveApi 玩家移动API
type MoveApi struct {
	znet.BaseRouter
}

func (m *MoveApi) Handle(request ziface.IRequest) {
	//解析客户端传递过来的proto协议
	protoMsg := &pb.Position{}
	err := proto.Unmarshal(request.GetData(), protoMsg)
	if err != nil {
		fmt.Println("move proto unmarshal err!")
		return
	}
	//获取当前玩家ID
	pid, err := request.GetConnection().GetProperty("pid")
	if err != nil {
		fmt.Println("GetProperty pid err!")
		return
	}
	fmt.Printf("Player pid = %d, move(%f, %f, %f, %f)\n", pid, protoMsg.X, protoMsg.Y, protoMsg.Z, protoMsg.V)
	//给其他玩家进行当前玩家的信息广播
	player := core.WorldManagerObj.GetPlayerByPid(pid.(int32))
	player.UpdatePos(protoMsg.X, protoMsg.Y, protoMsg.Z, protoMsg.V)

}
